#!/bin/bash
cd `dirname $0`
FPID=pid
PID=0
if [ -f "$FPID" ];
then
    PID=`sed '/pid/!d;s/.*=//' $FPID | tr -d '\r'`
    echo "the server is already started at pid:" $PID
    exit 1
fi
if [ ! -z $CLASSPATH ]; then
    CP=$CLASSPATH
fi
nohup java  -jar ../server.jar --spring.config.location=../resources/ --spring.profiles.active=prod & PID=$!
TIME=10
COUNT=0
while [ $COUNT -lt 1 ]; do
    sleep 1
    echo "starting..."
    COUNT=`ps -f | grep java | grep $PID | awk '{print $2}' | wc -l`
    if [ $COUNT -gt 0 ]; then
        break
    fi
    let TIME=$TIME-1
    if [ $TIME -eq 0 ]; then
        echo  "$RED_COLOR" && echo "start timeout" && echo  "$RES"
        exit 1
    fi
done
echo "pid=$PID" > pid
echo "PID: $PID"
echo "ok"